//
//  ASSearchHotCell.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASSearchHotCell.h"

static NSString *ASSearchHotSubCellStr = @"ASSearchHotSubCell";

@interface ASSearchHotSubCell : UICollectionViewCell

@property (strong, nonatomic) UILabel *numLab;
@property (strong, nonatomic) UILabel *contentLab;
@property (assign, nonatomic) SearchHotType type;

@end

@implementation ASSearchHotSubCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.numLab];
        [self.contentView addSubview:self.contentLab];
        
        @weakify(self);
        [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.left.mas_equalTo(16);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.centerY.mas_equalTo(self.mas_centerY);
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(-14);
        }];
    }
    return self;
}

- (UILabel *)numLab{
    if (!_numLab) {
        _numLab = [UILabel new];
        _numLab.font = [UIFont systemFontOfSize:14];
        _numLab.textColor = [UIColor colorWithHexString:@"#FC6783"];
    }
    return _numLab;
}

- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [UILabel new];
        _contentLab.font = [UIFont systemFontOfSize:14];
        _contentLab.textColor = [UIColor colorWithHexString:@"#1a1a1a"];
    }
    return _contentLab;
}

- (void)setType:(SearchHotType)type{
    _type = type;
    if (type == SearchHotType_history) {
        self.numLab.hidden = YES;
        self.contentLab.textColor = [UIColor colorWithHexString:@"#808080"];
        [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
        }];
    }else{
        self.numLab.hidden = NO;
        self.contentLab.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
        [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
        }];
    }
}
@end


@implementation ASSearchHotCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.scrollDirection = UICollectionViewScrollDirectionVertical;
        fl.minimumLineSpacing = 0;
        fl.minimumInteritemSpacing = 0;
        fl.itemSize = CGSizeMake(SCREEN_WIDTH / 2, 33);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = self.contentView.backgroundColor;
        [_collectionView registerClass:[ASSearchHotSubCell class] forCellWithReuseIdentifier:ASSearchHotSubCellStr];
    }
    return _collectionView;
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASSearchHotSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASSearchHotSubCellStr forIndexPath:indexPath];
    if (self.dataArr.count > indexPath.row) {
        NSString *model = self.dataArr[indexPath.row];
        cell.numLab.text = [NSString stringWithFormat:@"%zd",indexPath.row+1];
        cell.contentLab.text = model;
        cell.type = self.type;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        NSString *model = self.dataArr[indexPath.row];
        if (self.clickSubSearchBlock) {
            self.clickSubSearchBlock(model,indexPath.row,self.type);
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
